﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace TVC_WEB.Global
{
    public partial class prolist : System.Web.UI.Page
    {
        string sErr = "";
        public string GetCategoryPath(string cate)
        {
            string result = "";//"<a href=\"/\">Home</a>&nbsp;&nbsp;";
            string[] arrCare = cate.Split(',');
            BUS.CategoriesControl ct = new BUS.CategoriesControl();
            for (int i = 0; i < arrCare.Length; i++)
            {
                DTO.CategoriesInfo inf = ct.Get(Convert.ToInt32(arrCare[i]), ref sErr);
                if (inf.Type == "Category")
                {
                    lbCateName.Text = inf.NameVN;
                    DataTable dt = ct.GetAllParents(inf.ID, ref sErr);
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        DataRow row = dt.Rows[j];
                        if (j == 0)
                            result = "<b>" + row["NameVN"].ToString() + "</b>" + result;
                        else
                            result = "<a href='prolist.aspx?cate=" + row["ID"].ToString() + "&catetype=Category'>" + row["NameVN"].ToString() + "</a> &nbsp;&nbsp;&gt;&nbsp;&nbsp;" + result;
                    }
                    return "<a href='/Global/'>Home</a>&nbsp;&nbsp;&gt;&nbsp;&nbsp;" + result;

                }
            }
            return "<a href='/Global/'>Home</a>";
        }
        public string GetImage(string imageName)
        {
            string strUploadFolder = "/" + ConfigurationManager.AppSettings["UploadFolder"].ToString() + "Images/";
            return strUploadFolder + imageName;
        }
        public string GetPrice(string imageName)
        {
            return Convert.ToDouble(imageName).ToString("N0") + "đ";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                LoadProducts();
                if (Request["cate"] != null)
                {
                    lbCatePath.Text = GetCategoryPath(Request["cate"].ToString());
                    string qury = Request.Url.PathAndQuery;
                    Session["currentshop"] = qury;
                    Session["cate"] = Request["cate"].ToString();
                }
                else
                {
                    lbCatePath.Text = "<a href='/Global/'>Home</a>";
                }

            }
        }

        private void LoadProducts()
        {
            //if (Session["dt_product"] != null)
            //{
            //    DataTable dt = (DataTable)Session["dt_product"];
            //    if (Request["content"] != null)
            //    {
            //        dt.DefaultView.RowFilter = "NameVN like '%" + Request["content"] .ToString()+ "%'";
            //    }
            //    dt.DefaultView.Sort = hdSort.Value;
            //    lvPro.DataSource = dt;
            //    lvPro.DataBind();
            //    Session["dt_product"] = null;
            //}
            //else
            //{
            if (Request["cate"] != null)
            {
                BUS.CategoriesControl ctctr = new BUS.CategoriesControl();
                BUS.ProductsControl proCtr = new BUS.ProductsControl();
                string[] arrTmp = Request["cate"].ToString().Split(',');
                string arrid = "";
                for (int i = 0; i < arrTmp.Length; i++)
                {
                    string tmp = ctctr.GetAllChilds(arrTmp[i], ref sErr);
                    if (i == 0)
                        arrid = proCtr.GetIDByArrCate(tmp, ref sErr);
                    else arrid = proCtr.GetIDByArrCate(arrid, tmp, ref sErr);
                }

                DataTable dt = proCtr.GetInID(arrid, ref sErr);
                if (Request["content"] != null)
                {
                    dt.DefaultView.RowFilter = "NameVN like '%" + Request["content"].ToString() + "%'";
                }
                dt.DefaultView.Sort = hdSort.Value;
                lvPro.DataSource = dt;
                lvPro.DataBind();
            }
            else
            {
                BUS.ProductsControl proCtr = new BUS.ProductsControl();
                DataTable dt = proCtr.GetAll(ref sErr);
                if (Request["content"] != null)
                {
                    dt.DefaultView.RowFilter = "NameVN like '%" + Request["content"].ToString() + "%'";
                }
                dt.DefaultView.Sort = hdSort.Value;
                lvPro.DataSource = dt;
                lvPro.DataBind();
            }
            //       }
        }

        protected void DataPager_PreRender(object sender, EventArgs e)
        {
            LoadProducts();
        }

        protected void lkName_Click(object sender, EventArgs e)
        {
            hdSort.Value = "NameVN";
        }

        protected void lbNewest_Click(object sender, EventArgs e)
        {
            hdSort.Value = "BEN_S";
        }

        protected void lkPriceAsc_Click(object sender, EventArgs e)
        {
            hdSort.Value = "NewPrice ASC";
        }

        protected void lkPriceDesc_Click(object sender, EventArgs e)
        {
            hdSort.Value = "NewPrice DESC";
        }

        protected void lbTopSell_Click(object sender, EventArgs e)
        {
            hdSort.Value = "BEN_S";
        }
    }
}
